from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.utils.timezone import now

# Create your models here.

class User(AbstractUser):
    username = models.CharField('用户名', max_length=32, unique=True)
    password = models.CharField('密码', max_length=128)
    phone = models.CharField('手机号', max_length=11, unique=True, blank=True, null=True,
                             validators=[RegexValidator(regex=r'^1[3-9]\d{9}$', message='请输入正确的手机号格式')])
    email = models.EmailField('邮箱', blank=True, null=True, unique=True)

    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return self.username


class UserProfile(models.Model):
    GENDER_CHOICES = [
        ('M', '男'),
        ('F', '女'),
        ('O', '其他'),
    ]

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile', verbose_name='用户')
    nickname = models.CharField('昵称', max_length=50, blank=True, null=True)
    avatar = models.URLField('头像', max_length=500, blank=True, null=True)
    gender = models.CharField('性别', max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
    birthday = models.DateField('生日', blank=True, null=True)
    bio = models.TextField('个人简介', max_length=500, blank=True, null=True)
    location = models.CharField('所在地', max_length=100, blank=True, null=True)
    website = models.URLField('个人网站', blank=True, null=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'user_profile'
        verbose_name = '用户信息'
        verbose_name_plural = '用户信息'

    def __str__(self):
        return f"{self.user.username}的资料"


class ThirdPartyLogin(models.Model):
    PLATFORM_CHOICES = [
        ('wechat', '微信'),
        ('alipay', '支付宝'),
        ('github', 'GitHub'),
        ('gitee', 'Gitee'),
        ('dingtalk', '钉钉'),
        ('feishu', '飞书'),
        ('qq', 'QQ'),
    ]

    platform = models.CharField('平台', max_length=20, choices=PLATFORM_CHOICES)
    platform_user_id = models.CharField('三方平台用户ID', max_length=100)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户', related_name='third_party_logins')
    access_token = models.TextField('访问令牌', blank=True, null=True)
    refresh_token = models.TextField('刷新令牌', blank=True, null=True)
    expires_at = models.DateTimeField('令牌过期时间', blank=True, null=True)
    platform_user_info = models.JSONField('平台用户信息', blank=True, null=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'third_party_login'
        verbose_name = '第三方登录'
        verbose_name_plural = '第三方登录'
        unique_together = ('platform', 'platform_user_id')

    def __str__(self):
        return f"{self.platform} - {self.user.username}"